<?php
	include_once('../include/init.php');
	include_once(dirname(dirname(__FILE__)).'/include/global_function.php'); //绝对路径
	//include_once(dirname(dirname(__FILE__)).'/include/fun_type.php'); //无极分类递归函数
	$act = !empty($_GET['act'])?$_GET['act']:'list';
	$db = new Type();
	switch($act)
	{
		case 'list':
		
		$typeArr = $db->getTree();
		//print_r($typeArr);exit;
		include_once('templates/type.php');
		
		break;
		//添加 add
		
		case 'add':
			if(!empty($_POST)){
				//print_r($_POST);exit;
				if($db->create($_POST)){
					echo ('<script>
							alert("添加成功！");
							window.location = "type.php";
						</script>');
				}
				else{
					die('查询出错，原因：'.mysql_error());
				}
				exit;
			}
			$top = $db->getTreeTop();//获取顶级分类
			//print_r($top);exit;
			
			include_once('templates/type_add.php');
		break;
		
		case 'getLevel':
			$pid =!empty($_GET['pid'])?$_GET['pid']:0;
			$levelList = $db->getTreeTop($pid);
		
			echo json_encode($levelList);
			

		break;
		//修改
		case 'edi':
		if(!empty($_POST)){
		//print_r($_POST);exit;
			$id = $_POST['id'];
			if($db->update($_POST)){
				echo ('<script>
							alert("修改成功！");
							window.location = "type.php";
						</script>');
			}
			exit;
		}
		$id = !empty($_GET['id'])?$_GET['id']:0;//获取Id
		$db->where = array('id'=>$id);
		$result = $db->getOne();
		$pid = $result['pid'];
		$parent_list = array();
		while($pid !=0){
			//print_r($pid);exit;
			$db->where = array('id'=>$pid);
			$p_level = $db->getOne();//获取上一级数据
			//print_r($p_level);exit;
			$p_pid = $p_level['pid'];
			//print_r($p_pid);exit;
			$p_level_list = $db->getTreeTop($p_pid);//获取上一级列表
			//print_r($p_level_list);exit;
			$parent_list[$pid] = $p_level_list;
			//print_r($parent_list[$pid]);exit;
			$pid = $p_pid;
			//print_r($pid);exit;
		}
		$parent_list = array_reverse($parent_list,true);//数组倒叙
		include_once('templates/type_edit.php');
		break;
		
		//删除
		case 'del':
		$id = !empty($_GET['id'])?$_GET['id']:0;
		if(! $id || ''==$id){
			die('
				<script>
					alert("id参数有误！")
					window.history.back();
				<script/>
			');
		}
		$db->typeDelete($id);
		$sql = 'delete from `12_type` where `id`="'.$id.'"';
		$query = mysql_query($sql);
		if(!$query){
			die('执行失败：'.mysql_error());
		}
		else{
			die('<script>
						alert("删除成功！");
						window.history.back();
				     </script>');
		}

		
		break;
		
		default:
		echo "参数错误";
	}

?>
